package com.qianbei.common.view.foot;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qianbei.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页水平滚动视图适配器
 * 
 * @author gml
 */

public class HorizontalScrollAdapter extends BaseAdapter {
	private List<JSONObject> jsonList = new ArrayList<JSONObject>();
	private Context mContext;

	// 选中时候显示的图片
	private Integer ImageSoureArry[] = {};
	// 未选中时候的图片
	private Integer ImageSoureArry2[] = {};

	private String[] mainString;

	public HorizontalScrollAdapter(Context c) {
		mContext = c;
		mainString = mContext.getResources().getStringArray(
				R.array.home_item_string);
		JSONObject jsonObject;
		try {
			for (int i = 0; i < mainString.length; i++) {
				jsonObject = new JSONObject();

				jsonObject.put("explain", mainString[i]);

//				jsonObject.put("imageResId", ImageSoureArry2[i]);
				jsonList.add(jsonObject);
			}
			notifyDataSetChanged();

		} catch (JSONException e) {
			e.printStackTrace();
		}

	}

	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHoders viewHoder;
		// 防止重复生成视图
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.bottom_navigation_item, null);
			viewHoder = new ViewHoders();
			viewHoder.imageView = (ImageView) convertView
					.findViewById(R.id.imageView);
			viewHoder.textView = (TextView) convertView
					.findViewById(R.id.textView);
			convertView.setTag(viewHoder);
		} else {
			viewHoder = (ViewHoders) convertView.getTag();
		}

		JSONObject jsonObject = jsonList.get(position);

		viewHoder.imageView
				.setImageResource(jsonObject.optInt("imageResId", 0));
		viewHoder.textView.setText(jsonObject.optString("explain", ""));
		if (position == 0) {
			viewHoder.textView.setTextColor(Color.parseColor("#5ed8ff"));
		}
		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				setClickViewSource(v.getId());

				if (callback != null) {
					callback.ItemClick(v.getId());
				}

			}
		});

		return convertView;
	}

	/**
	 * 设置点击选中的样式
	 */

	private void setClickViewSource(int id) {

		View previousView = ((Activity) mContext).findViewById(id);

		ImageView imageView = (ImageView) previousView
				.findViewById(R.id.imageView);
		TextView textView = (TextView) previousView.findViewById(R.id.textView);
		textView.setTextColor(Color.parseColor("#5ed8ff"));
		int ids = HorizontalScrollLayout.iDlist.indexOf(id);
		imageView.setImageResource(ImageSoureArry[ids]);
	}

	/**
	 * 还原 前一个 按钮的样式
	 * 
	 * @param imageView
	 */
	public void setImageViewResource(int previousViewId) {

		View previousView = ((Activity) mContext).findViewById(previousViewId);
		ImageView imageView = (ImageView) previousView
				.findViewById(R.id.imageView);
		TextView textView = (TextView) previousView.findViewById(R.id.textView);
		textView.setTextColor(Color.parseColor("#eeeeee"));
		int ids = HorizontalScrollLayout.iDlist.indexOf(previousViewId);
		imageView.setImageResource(ImageSoureArry2[ids]);
	}

	class ViewHoders {
		TextView textView;
		ImageView imageView;
	}

	public ItemCallback callback;

	public interface ItemCallback {
		void ItemClick(int id);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return jsonList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return jsonList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

}